"""
httpbin网站: 用于http测试很方便， 返回的数据是json格式
"""
import simplejson
import ssl
from urllib import parse
from urllib.request import urlopen, Request

base_url = 'http://www.httpbin.org/post'
params = {'name': '春龙,@#$%', 'age': '25'}

# urlopen发送http请求
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
request = Request(base_url, headers={
    'User-agent': user_agent
})

# encode(): 转换string为bytes
with urlopen(request, data=parse.urlencode(params).encode(), context=ssl._create_unverified_context()) as response:
    response_text = response.read()
    json_text = simplejson.loads(response_text)
    print(type(json_text))
    print(json_text)
